package com.mall.service;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.dao.mapper.OrderMapper;
import com.mall.model.CommClass;
import com.mall.model.Order;
import com.mall.model.OrderExample;
import com.mall.model.OrderExample.Criteria;
import com.mall.model.Seckill;
import com.mall.model.TimesExample;

@Service
public class OrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private CommClassService commClassService;
	@Autowired
	private SeckillService seckillService;
	/**
	 * 获取订单集合
	 * 
	 * @param page
	 *
	 * @dagewang 2017年8月16日
	 */
	public List<Order> listOrder(Integer state, Integer page) throws Exception {
		OrderExample ex = new OrderExample();
		ex.createCriteria().andStateEqualTo(state);
		ex.setOrderByClause("cdate desc");
		// 分页
		RowBounds rowBounds = new RowBounds(page * 8, 8);
		List<Order> list = null;
		try {
			list = orderMapper.selectByExampleWithRowbounds(ex, rowBounds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取所有订单(重载)
	 * 
	 * @param page
	 *
	 * @dagewang 2017年8月16日
	 */
	public List<Order> listOrder(Integer page) throws Exception {
		OrderExample ex = new OrderExample();
		ex.setOrderByClause("cdate desc");
		// 分页
		RowBounds rowBounds = new RowBounds(page * 8, 8);
		List<Order> list = null;
		try {
			list = orderMapper.selectByExampleWithRowbounds(ex, rowBounds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取所有订单重载(查看是否有新订单)
	 * 
	 * @param page
	 *
	 * @dagewang 2017年8月16日
	 */
	public boolean listOrder() throws Exception {
		TimesExample ex = new TimesExample();
		Date date = new Date();
		long time = date.getTime() - 65000;
		Date date2 = new Date(time);
		ex.setDatesta(date2);
		List<Order> list = null;
		list = orderMapper.selectByExampleTime(ex);
		if (list.size() != 0) {
			return true;
		}
		return false;
	}

	/**
	 * 获取单个订单
	 *
	 * @dagewang 2017年8月19日
	 */
	public Order get(Integer id) {
		Order selectByPrimaryKey = orderMapper.selectByPrimaryKey(id);

		return selectByPrimaryKey;

	}

	public OrderMapper getOrderMapper() {
		return orderMapper;
	}

	public void setOrderMapper(OrderMapper orderMapper) {
		this.orderMapper = orderMapper;
	}

	/**
	 * 更新订单状态
	 *
	 * @dagewang 2017年8月18日
	 */
	public int update(Integer id, Integer state) {
		Order order = this.get(id);
		order.setState(state);
		order.setUdate(new Date());
		int updateByPrimaryKey = orderMapper.updateByPrimaryKey(order);
		return updateByPrimaryKey;
	}

	/**
	 * 更新订单状态重载
	 *
	 * @dagewang 2017年8月18日
	 */
	public int update(Integer id, Integer state, String remarks,Integer p) {
		Order order = this.get(id);
		if(order==null){
			return 0;
		}
		order.setState(state);
		order.setUdate(new Date());
		if(p==null){
			order.setReason(remarks);
		}else{
			order.setTransport(remarks);
		}
		int updateByPrimaryKey = orderMapper.updateByPrimaryKeySelective(order);
		return updateByPrimaryKey;
	}

	/**
	 * 更新订单状态重载
	 *
	 * @dagewang 2017年8月18日
	 */
	public int update(Integer id, Integer state, Integer staffid) {
		Order order = this.get(id);
		order.setState(state);
		order.setUdate(new Date());
		order.setStaffid(staffid);
		int updateByPrimaryKey = orderMapper.updateByPrimaryKey(order);
		return updateByPrimaryKey;
	}
	/**
	 * 添加订单
	 * 
	 * @dagewang 2017年9月6日
	 */
	public int save(Order order,Integer seckill) throws Exception {
		if(seckill!=null&&!seckill.equals("")){
			Seckill kill=seckillService.get(seckill);
			order.setPrice(kill.getPrice());
		}else{
			CommClass comm = commClassService.getCommById(order.getClassid());
			if (comm == null || comm.getStock() == 0) {
				return 0;
			} else if ((comm.getStock() - order.getNum()) < 0) {
				return 0;
			} else {
				comm.setStock(comm.getStock() - order.getNum());
			}
			order.setPrice(comm.getPrice());
			commClassService.update(comm);
		}
		order.setCdate(new Date());
		order.setState(0);
		order.setTransportstate(0);
		order.setChecksee(0);
		int insert = orderMapper.insert(order);
		return insert;
	}

	public CommClassService getCommClassService() {
		return commClassService;
	}

	public void setCommClassService(CommClassService commClassService) {
		this.commClassService = commClassService;
	}

	/**
	 * 根据用户id查询订单
	 * 
	 * @param state
	 *
	 * @dagewang 2017年9月11日
	 */
	public List<Order> listOrderByUserId(Integer userid, Integer page, Integer state) throws Exception {
		OrderExample ex = new OrderExample();
		ex.setOrderByClause("cdate desc");
		if (state == 0) {
			ex.createCriteria().andUseridEqualTo(userid);
		} else if (state == 1) {
			Criteria createCriteria = ex.createCriteria();
			createCriteria.andUseridEqualTo(userid);
			createCriteria.andStateLessThan(3);
		} else {
			Criteria createCriteria = ex.createCriteria();
			createCriteria.andUseridEqualTo(userid);
			createCriteria.andStateEqualTo(3);
		}
		RowBounds rowBounds = new RowBounds(page * 8, 8);
		List<Order> selectByExample = orderMapper.selectByExampleWithRowbounds(ex, rowBounds);
		return selectByExample;
	}

	/**
	 * 删除订单
	 *
	 * @dagewang 2017年9月12日
	 */
	public Integer delete(Integer id) throws Exception {
		return orderMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 取消订单
	 *
	 * @dagewang 2017年9月12日
	 */
	public Order cancel(Integer id, String content) throws Exception {
		Order selectByPrimaryKey = orderMapper.selectByPrimaryKey(id);
		if (selectByPrimaryKey != null) {
			selectByPrimaryKey.setReason(content);	
			selectByPrimaryKey.setState(4);
			int updateByPrimaryKeySelective = orderMapper.updateByPrimaryKeySelective(selectByPrimaryKey);
			if (updateByPrimaryKeySelective != 0) {
				return selectByPrimaryKey;
			}
		}
		return null;
	}

	public SeckillService getSeckillService() {
		return seckillService;
	}

	public void setSeckillService(SeckillService seckillService) {
		this.seckillService = seckillService;
	}
	

}
